import { BaseEntity } from './BaseEntity';

import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';

import { SysUser } from './SysUser';

@Entity()
export class SysLogger extends BaseEntity {
  @Index()
  @Column({
    type: 'varchar',
    nullable: true,
    default: 'other',
    comment: '日志分类'
  })
  category?: string;

  @Column({
    type: 'varchar',
    comment: '请求地址',
    nullable: true,
    name: 'request_uri'
  })
  requestUri?: string;

  @Column({
    type: 'int',
    comment: '请求响应码',
    default: 200,
    name: 'response_code'
  })
  responseCode?: string;

  @Column({
    type: 'varchar',
    nullable: true,
    comment: '请求形式（HTTP OR HTTPS）'
  })
  scheme?: string;

  @Column({
    type: 'varchar',
    nullable: true,
    comment: '请求类型'
  })
  method?: string;

  @Column({
    type: 'varchar',
    nullable: true,
    name: 'client_host',
    comment: '客户端Host'
  })
  clientHost?: string;

  @Column({
    type: 'varchar',
    nullable: true,
    name: 'client_ip',
    comment: '客户端Ip'
  })
  clientIp?: string;

  @Column({
    type: 'datetime',
    name: 'start_time',
    nullable: true,
    comment: '请求开始时间'
  })
  startTime?: Date;

  @Column({
    type: 'datetime',
    name: 'end_time',
    nullable: true,
    comment: '请求结束时间'
  })
  endTime?: Date;

  @Column({
    type: 'int',
    nullable: true,
    comment: '响应时长'
  })
  duration?: number;

  @Column({
    type: 'blob',
    comment: '请求参数(query)',
    nullable: true,
    name: 'query_params'
  })
  queryParams: unknown;

  @Column({
    type: 'blob',
    comment: '请求参数(body)',
    nullable: true,
    name: 'request_body'
  })
  requestBody: unknown;

  @Column({
    type: 'blob',
    comment: '响应内容(body)',
    nullable: true,
    name: 'response_body'
  })
  responseBody: unknown;

  @ManyToOne(() => SysUser, {
    onDelete: 'SET NULL',
    createForeignKeyConstraints: false,
    nullable: true
  })
  @JoinColumn({
    name: 'operator_id'
  })
  @Index()
  operatorId: SysUser;
}
